<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="店铺名称" prop="shopName">
            <el-input v-model="queryParams.shopName" placeholder="请输入店铺名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="登录名称" prop="loginName">
            <el-input v-model="queryParams.loginName" placeholder="请输入登录名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="商户名称" prop="merName">
            <el-input v-model="queryParams.merName" placeholder="请输入商户名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['merchant:shop:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="shopList" class="tableEmpty">
        <el-table-column label="店铺名称" align="center" prop="shopName" />
        <el-table-column label="登录名称" align="center" prop="loginName" />
        <el-table-column label="商家名称" align="center" prop="merName" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template #default="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="查看" placement="top">
              <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['merchant:shop:view']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 查询店铺信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="900px" append-to-body>
        <ShopDetail :shop-id="shopId" />
    </el-dialog>
  </div>
</template>

<script setup name="Shop" lang="ts">
import { listShop, getShop } from '@/api/merchant/shop';
import ShopDetail from './view.vue'

import { ShopVO, ShopQuery, ShopForm } from '@/api/retail/shop/types';

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));

const shopList = ref<ShopVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const shopFormRef = ref<ElFormInstance>();
const shopId = ref();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ShopForm = {
  id: undefined,
  shopName: undefined,
  loginName: undefined,
  merId: undefined,
  email: undefined,
  status: "0",
}
const data = reactive<PageData<ShopForm, ShopQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shopName: undefined,
    loginName: undefined,
    merId: undefined,
    status: undefined,
    params: {
    }
  },
  rules: {
    merName: [
      { required: true, message: "商户名称不能为空", trigger: "blur" }
    ],
    shopName: [
      { required: true, message: "商店名称不能为空", trigger: "blur" }
    ],
    loginName: [
      { required: true, message: "登录名称不能为空", trigger: "blur" }
    ],
    email: [
      { required: true, message: "邮件地址不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商铺信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listShop(queryParams.value);
  shopList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  shopFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 修改按钮操作 */
const handleView = async (row?: ShopVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  //const res = await getShop(_id);
  //Object.assign(form.value, res.data);
  shopId.value = _id;
  dialog.visible = true;
  dialog.title = "查看店铺信息";
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('retail/shop/export', {
    ...queryParams.value
  }, `shop_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
